<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

use Illuminate\Database\Query;
// require_once '../app/libary/captcha/Code.class.php';

class GlobalController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/global/index';
    protected $typeleve=1;
    protected $typeleve_default=1;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){

        // $list = BackstageModel::getGlobal();
        // dd($list);exit;
        // return view('backstage/global/index',['list'=>$list]);

        $data = BackstageModel::getGlobal();

        foreach( $data as $key => $value ){
            switch( $value->type ){
                case 'input':
                    $data[$key] -> _html = '<input type="text" class="lg" name="'.$value->code.'" value="'.$value->text.'"><span style="color: darkred;"><i class="fa fa-exclamation-circle yellow"></i>'.$value->tip.'</span>';
                    break;
                case 'textarea':
                    $data[$key] -> _html = '<textarea class="lg" style="display: inline-block; float: left;" name="'.$value->code.'">'.$value->text.'</textarea><span class="textarea-lg" style="color: darkred;"><i class="fa fa-exclamation-circle yellow"></i>'.$value->tip.'</span>';
                    break;
            }
        }

        return view('backstage/global/index',compact('data'));
    }
    public function add(){
        $setting_type = config('app.setting_type');
        return view('backstage/global/add',['setting_type'=>$setting_type]);
    }
    public function doadd(Request $request){
        if($request->isMethod('POST')){
            
            $request = $request->all();
            
            $data['name'] = trim($request['name']);
            $data['code'] = trim($request['code']);

            $data['type'] = trim($request['type']);
            $data['prmtype'] = intval($request['prmtype']);
            $data['tip'] = trim($request['tip']);
            $data['sort_order'] = intval($request['sort_order']);
            $newid = CommonModel::addDataGetId('global',$data);
            if($newid == true){
                $arrJson = array('status' => 1, 'message'=>'慘數添加成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'慘數添加失敗');
            }
            return json_encode($arrJson);
        }
    }
 
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            
            $data = $request->except('_token');
            // return response()->json(array('status' => 0, 'message'=>'修改失敗'));
            $s = 0;
            $f = 0;
            
            foreach ($data as $k => $v) {
            //     $oldValue = CommonModel::getField('global','text',['code'=>'app_version']);
            // dd($oldValue);exit;
                if($k!='_token'){
                    $oldValue = CommonModel::getField('global','text',['code'=>$k]);
                    // dd($oldValue);exit;
                    if($oldValue!=$v){
                        // dd($k);dd($v);exit;
                        $save = CommonModel::updateData('global',['code'=>$k],['text'=>$v]);
                        if($save){
                            $s++;
                        }else{
                            $f++;
                        }
                    }
                }
            }
            if($f == 0){
                $arrJson = array('status' => 1, 'message'=>'修改成功');
            }elseif($s == 0){
                $arrJson = array('status' => 0, 'message'=>'修改失敗');
            }else{
                $arrJson = array('status' => 2, 'message'=>'成功：'.$s.'，失敗：'.$f);
            }
            return json_encode($arrJson);
        }
    }

    
    


}
